/*
 * Copyright (C) 2011 Domenico Briganti <dometec@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
* @autor: Domenico Briganti
* @since: 06/02/2009
*
*/

if (typeof BussinessObject == "undefined" || !BussinessObject) {
	BussinessObject = {};
};

if (typeof BussinessMethos == "undefined" || !BussinessMethos) {
	BussinessMethos = {};
};

BussinessMethos.genericCallback = {
    success: function(mess) {
		//Do nothing
	},
	failure: function(mess) {
        onErrorMessage("Errore");
    }   
};

/****Dichiarazioni oggetti di bussiness e access method *****/
BussinessObject.Environment = function (data) {
	
	/*
	 * Esempio di xml di Environment
	 * <environment>
	 * 		<name>XXXX</name>
	 * 		<numVirtualHost>XXXX</numVirtualHost>
	 * </environment>
	 */
	this.todelete = false;
	this.isnew = false;
	
	if (data == null) {
		this.isnew = true;
		var xhr = YAHOO.util.Connect.createXhrObject();
		var conn = xhr.conn;
		conn.open("GET", server + "manager/environment/new", false);
		YAHOO.util.Connect.setHeader(xhr);
		conn.send(null);
		this.data = conn.responseXML;		
	} else if (typeof data == 'string') {
		var xhr = YAHOO.util.Connect.createXhrObject();
		var conn = xhr.conn;
		conn.open("GET", server + "manager/environment/" + data, false);
		YAHOO.util.Connect.setHeader(xhr);
		conn.send(null);
		this.data = conn.responseXML;
	}
	
	this.setToDelete = function(bool) {
		this.todelete = bool;
	};
	
	this.isToDelete = function() {
		return this.todelete;
	};
	
	this.isNew = function() {
		return this.isnew;
	};
	
	this.getData = function() {
		return this.data;
	};
	
	this.getName = function() {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getNumVirtualHost = function () {
		var nodes = evalutateXPath(this.data.firstChild, "numVirtualHost");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
		
	this.setName = function(name) {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			setContentInElement(nodes[0], name);
		else {
			var node = this.data.createElement("name");
			setContentInElement(node, name);
			this.data.firstChild.appendChild(node);
		}
	};
	 
};

BussinessObject.VirtualHost = function (data) {
	
   /*
	* Esempio di xml di Environment
	*	<virtualhost>
	*	  <environment>ambiente</environment>
	*	  <name>nome</name>
	*	  <numWebApps>3</numWebApps>
	*	  <deploymentMethods>
	*	    <string>/c/gsdfgs dfsdfg sd<string>
	*	    <string>http://www.loc.ii:8080/manager<string>
	*	  </deploymentMethods>
	*	  <jmxServersToMonitor>
	*	    <string>cucu:89898</string>
	*	    <string>cucu2:89898</string>
	*	  </jmxServersToMonitor>
	*	</virtualhost>
	* 
	* data è formato dalla concatenazione di environment e name con in mezzo una ","
	* i punti sono trasformati in ":"
	* 
	*/
	this.todelete = false;
	this.isnew = false;
	
	if (data == null) {
		this.isnew = true;
		var xhr = YAHOO.util.Connect.createXhrObject();
		var conn = xhr.conn;
		conn.open("GET", server + "manager/virtualhost/new", false);
		YAHOO.util.Connect.setHeader(xhr);
		conn.send(null);
		this.data = conn.responseXML;		
	} else if (typeof data == 'string') {
		var xhr = YAHOO.util.Connect.createXhrObject();
		var conn = xhr.conn;
		conn.open("GET", server + "manager/virtualhost/" + data, false);
		YAHOO.util.Connect.setHeader(xhr);
		conn.send(null);
		this.data = conn.responseXML;
	};
	
	this.setToDelete = function(bool) {
		this.todelete = bool;
	};
	
	this.isToDelete = function() {
		return this.todelete;
	};
	
	this.isNew = function() {
		return this.isnew;
	};
	
	this.getData = function() {
		return this.data;
	};
	
	this.getEnvironment = function() {
		var nodes = evalutateXPath(this.data.firstChild, "environment");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};

	this.setEnvironment = function(environment) {
		var nodes = evalutateXPath(this.data.firstChild, "environment");
		if (nodes.length > 0)
			setContentInElement(nodes[0], environment);
		else {
			var node = this.data.createElement("environment");
			setContentInElement(node, environment);
			this.data.firstChild.appendChild(node);
		}
	};
	
	this.getName = function() {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};

	this.setName = function(name) {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			setContentInElement(nodes[0], name);
		else {
			var node = this.data.createElement("name");
			setContentInElement(node, name);
			this.data.firstChild.appendChild(node);
		}
	};
	
	this.getNumWebApps = function () {
		var nodes = evalutateXPath(this.data.firstChild, "numWebApps");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getDeploymentMethods = function() {
		var nodes = evalutateXPath(this.data.firstChild, "deploymentMethods");
		if (nodes.length == 0) {
			node = this.data.createElement("deploymentMethods");
			this.data.firstChild.appendChild(node)
			nodes = evalutateXPath(this.data.firstChild, "deploymentMethods");
		}
		return nodes[0];
	};
	
	this.getJMXServersToMonitor = function() {
		var nodes = evalutateXPath(this.data.firstChild, "jmxServersToMonitor");
		if (nodes.length == 0) {
			node = this.data.createElement("jmxServersToMonitor");
			this.data.firstChild.appendChild(node)
			nodes = evalutateXPath(this.data.firstChild, "jmxServersToMonitor");
		}
		return nodes[0];
	};
	
	this.addDeploymentMethod = function(deploymethod) {
		var nodes = evalutateXPath(this.data.firstChild, "deploymentMethods");
		var node = this.data.createElement("string");
		nodes[0].appendChild(node);
		setContentInElement(node, deploymethod);
	};
	
	this.removeDeploymentMethod = function(jmxServer) {
		var nodes = evalutateXPath(this.data.firstChild, "deploymentMethods/string");
		for (var i = 0; i < nodes.length; i++) {
			if (getContentInElement(nodes[i]) == jmxServer) {
				evalutateXPath(this.data.firstChild, "deploymentMethods")[0].removeChild(nodes[i]);
			}
		}
	};
	
	this.addJMXServersToMonitor = function(jmxServer) {
		var nodes = evalutateXPath(this.data.firstChild, "jmxServersToMonitor");
		var node = this.data.createElement("string");
		nodes[0].appendChild(node);
		setContentInElement(node, jmxServer);
	};
	
	this.removeJMXServerToMonitor = function(jmxServer) {
		var nodes = evalutateXPath(this.data.firstChild, "jmxServersToMonitor/string");
		for (var i = 0; i < nodes.length; i++) {
			if (getContentInElement(nodes[i]) == jmxServer) {
				evalutateXPath(this.data.firstChild, "jmxServersToMonitor")[0].removeChild(nodes[i]);
			}
		}
	};
	
};

BussinessObject.WebApp = function (data) {
	
   /*
	* Esempio di xml di webapp
	* <webapp> 
	*   <environment>MioInLocale</environment> 
	*   <virtualhost>www.dometec.it</virtualhost> 
	*   <name>contesto</name> 
	*   <lastdeploy>2009-12-29 13:17:59.168 CET</lastdeploy> 
	*   <deployMode>WAR</deployMode> 
	*   <currentDirectory></currentDirectory> 
	*   <scmCommand></scmCommand> 
	*   <buildCommandCommand></buildCommandCommand> 
	*   <pathArtifact></pathArtifact> 
	* </webapp>
	* 
	*/

	var xhr = YAHOO.util.Connect.createXhrObject();
	var conn = xhr.conn;
	conn.open("GET", server + "manager/webapp/" + data, false);
	YAHOO.util.Connect.setHeader(xhr);
	conn.send(null);
	this.data = conn.responseXML;
	
	this.getData = function() {
		return this.data;
	};
	
	this.getEnvironment = function() {
		var nodes = evalutateXPath(this.data.firstChild, "environment");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};

	this.getVirtualHost = function() {
		var nodes = evalutateXPath(this.data.firstChild, "virtualhost");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getName = function() {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};

	this.getDeployMode = function () {
		var nodes = evalutateXPath(this.data.firstChild, "deployMode");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	 
	this.getCurrentDirectory = function () {
		var nodes = evalutateXPath(this.data.firstChild, "currentDirectory");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getSCMCommand = function () {
		var nodes = evalutateXPath(this.data.firstChild, "scmCommand");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getBuildCommandCommand = function () {
		var nodes = evalutateXPath(this.data.firstChild, "buildCommandCommand");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getpathArtifact = function () {
		var nodes = evalutateXPath(this.data.firstChild, "pathArtifact");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
			
	this.setName = function(name) {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			setContentInElement(nodes[0], name);
		else {
			var node = this.data.createElement("name");
			setContentInElement(node, name);
			this.data.firstChild.appendChild(node);
		}
	};
	
};

BussinessObject.SvnProject = function (projectname) {
	
   /*
	* Esempio di xml di svnproject
	* <svnproject>
	*    <name>MioSitoDiProva</name>
	*    <url>svn://localhost/MioSitoDiProva/ContenutoStatico</url>
	*    <productRelease>URL: svn://localhost/MioSitoDiProva/ContenutoStatico/tags/release-0.0.1-start/htdocs</productRelease>
	* </svnproject>
	* 
	*/

	var xhr = YAHOO.util.Connect.createXhrObject();
	var conn = xhr.conn;
	conn.open("GET", server + "manager/svnproject/" + projectname, false);
	YAHOO.util.Connect.setHeader(xhr);
	conn.send(null);
	this.data = conn.responseXML;
	
	this.getData = function() {
		return this.data;
	};
	
	this.getName = function() {
		var nodes = evalutateXPath(this.data.firstChild, "name");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getEmails = function() {
		var nodes = evalutateXPath(this.data.firstChild, "emails");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};

	this.getNotifyAdded = function() {
		var nodes = evalutateXPath(this.data.firstChild, "notifyAdded");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getNotifyModified = function() {
		var nodes = evalutateXPath(this.data.firstChild, "notifyModified");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
	
	this.getNotifyRemoved = function() {
		var nodes = evalutateXPath(this.data.firstChild, "notifyRemoved");
		if (nodes.length > 0)
			return getContentInElement(nodes[0]);
		return null;
	};
			

	this.setEmails = function(emails) {
		var nodes = evalutateXPath(this.data.firstChild, "emails");
		if (nodes.length > 0)
			setContentInElement(nodes[0], emails);
		else {
			var node = this.data.createElement("emails");
			setContentInElement(node, emails);
			this.data.firstChild.appendChild(node);
		}
	};
	
	this.setNotifyAdded = function(boolean) {
		var nodes = evalutateXPath(this.data.firstChild, "notifyAdded");
		if (nodes.length > 0)
			setContentInElement(nodes[0], boolean);
		else {
			var node = this.data.createElement("notifyAdded");
			setContentInElement(node, boolean);
			this.data.firstChild.appendChild(node);
		}
	};
	
	this.setNotifyModified = function(boolean) {
		var nodes = evalutateXPath(this.data.firstChild, "notifyModified");
		if (nodes.length > 0)
			setContentInElement(nodes[0], boolean);
		else {
			var node = this.data.createElement("notifyModified");
			setContentInElement(node, boolean);
			this.data.firstChild.appendChild(node);
		}
	};
	
	this.setNotifyRemoved = function(boolean) {
		var nodes = evalutateXPath(this.data.firstChild, "notifyRemoved");
		if (nodes.length > 0)
			setContentInElement(nodes[0], boolean);
		else {
			var node = this.data.createElement("notifyRemoved");
			setContentInElement(node, boolean);
			this.data.firstChild.appendChild(node);
		}
	};
};

/**Funzioni di accesso remoto */
BussinessMethos.saveEnvironment = function (environment) {
  	
	if (environment.isToDelete()) { 		//DELETE HTTP METHOD - delete
		YAHOO.util.Connect.syncRequest("DELETE", server + "manager/environment/" + environment.getName(), BussinessMethos.genericCallback);
	} else if (environment.isNew()) { 		//POST HTTP METHOD - create
		YAHOO.util.Connect.syncRequest("POST", server + "manager/environment/", BussinessMethos.genericCallback, environment.getData());		
	} else { 								//PUT HTTP METHOD - modify
		YAHOO.util.Connect.syncRequest("PUT", server + "manager/environment/" + environment.getName(), BussinessMethos.genericCallback, environment.getData());
	}
		
};

BussinessMethos.saveVirtualHost = function (virtualhost) {
  	
	if (virtualhost.isToDelete()) { 		//DELETE HTTP METHOD - delete
		YAHOO.util.Connect.syncRequest("DELETE", server + "manager/virtualhost/" + virtualhost.getEnvironment() +  "," + virtualhost.getName().replace(new RegExp("\\.", "gi"), ":"), BussinessMethos.genericCallback);
	} else if (virtualhost.isNew()) { 		//POST HTTP METHOD - create
		YAHOO.util.Connect.syncRequest("POST", server + "manager/virtualhost/", BussinessMethos.genericCallback, virtualhost.getData());		
	} else { 								//PUT HTTP METHOD - modify
		YAHOO.util.Connect.syncRequest("PUT", server + "manager/virtualhost/" + virtualhost.getEnvironment() +  "," + virtualhost.getName().replace(new RegExp("\\.", "gi"), ":"), BussinessMethos.genericCallback, virtualhost.getData());
	}  
		
};

BussinessMethos.saveSvnProject = function (svnproject) {
	YAHOO.util.Connect.syncRequest("PUT", server + "manager/svnproject/" + svnproject.getName(), BussinessMethos.genericCallback, svnproject.getData());
};

BussinessMethos.getEnvironments = function () {
	return YAHOO.util.Connect.syncRequest("GET", server + "manager/environment/", BussinessMethos.genericCallback).conn.responseXML;
};

BussinessMethos.UndeployWebApp = function (environment, virtualhost, webapp) {
	return YAHOO.util.Connect.asyncRequest("DELETE", server + "services/Deployer?" + environment + "," + virtualhost.replace(new RegExp("\\.", "gi"), ":") + "," + webapp, BussinessMethos.genericCallback).conn.responseXML;
};

BussinessMethos.getChartSet = function () {
	return YAHOO.util.Connect.syncRequest("GET", server + "manager/chartset", BussinessMethos.genericCallback).conn.responseXML;
};

BussinessMethos.getDataPreview = function (url) {
	return YAHOO.util.Connect.syncRequest("GET", server + url, BussinessMethos.genericCallback).conn.responseXML;
};

BussinessMethos.logout = function () {
	return YAHOO.util.Connect.syncRequest("DELETE", server + "services/UserRoles", BussinessMethos.genericCallback).conn.responseXML;
};

